﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data;
using ServerVideoCall.DataSetLocalTableAdapters;
namespace ServerVideoCall
{
    class DataProvider
    {
        DataSetLocal dataSet = new DataSetLocal();
        private AdminTableAdapter adminTableAdater = new AdminTableAdapter();
        private MemberTableAdapter memberTableAdater = new MemberTableAdapter();
 
        // Khỏi Tạo Kết Nối
        private void GetData()
        {
            adminTableAdater.Fill(dataSet.Admin);
            memberTableAdater.Fill(dataSet.Member);
        }

        #region Kiểm Tra Tài Khoản Admin
        public int CheckAdmin(string user, string pass)
        {
            GetData();
            var ad = dataSet.Admin.SingleOrDefault(p => p.Username == user && p.Password == pass);
            if (ad != null)
            {
                if (user == "admin")
                    return 0;
                if (user == "superadmin")
                    return 1;
            }
            return 2;
        }
        #endregion

        #region Thay Đổi Mật Khẩu Admin
        public void ChangePasswordAdmin(string adm, string newpass)
        {
            GetData();
            dataSet.Admin.Single(p => p.Username == adm).Password = newpass;
            adminTableAdater.Update(dataSet.Admin);
        }
        #endregion

        #region Kiểm Tra Tài Khoản User Member | True - Thành Công : False - Thất Bại
        public bool CheckUsername(string user, string pass, string ip)
        {
            GetData();
            
            var us = dataSet.Member.SingleOrDefault(p=>p.Username == user && p.Password == pass && p.Ip == ip);
            // test Himachi bỏ check ip
            //var us = dataSet.Member.SingleOrDefault(p => p.Username == user && p.Password == pass);
            if (us != null)
                return true;
            return false;
        }
        #endregion

        #region Reset Mật Khẩu User
        public void Resetpassword(string user)
        {
            GetData();
            dataSet.Member.Single(p=>p.Username == user).Password = user;
            memberTableAdater.Update(dataSet.Member);
        }
        #endregion

        #region Đồi Mật Khẩu User
        public void ChangePasswordUser(string user, string newpass)
        {
            GetData();
            dataSet.Member.Single(p=>p.Username == user).Password = newpass;
            memberTableAdater.Update(dataSet.Member);
        }
        #endregion

        #region Thêm 1 User Mới : 1 - Trùng username : 2 - Trùng IP: 3 Thành Công
        public int InsertUser(string user, string pass, string ip)
        {
            GetData();
            var us = dataSet.Member.SingleOrDefault(p=>p.Username == user);
            if (us != null)
                return 1; // trung username
            else
            {
                var us2 = dataSet.Member.SingleOrDefault(p=>p.Ip == ip);
                if (us2 != null)
                    return 2; // trung IP
                else
                {
                    dataSet.Member.AddMemberRow(user, pass, ip);
                    memberTableAdater.Update(dataSet.Member);
                    return 3;
                }
            }
        }
        #endregion

        #region Xóa 1 user ra khỏi danh sách
        public void Remove(string user)
        {
            dataSet.Member.Single(p => p.Username == user).Delete();
            memberTableAdater.Update(dataSet.Member);
        }
        #endregion

        #region Xuất Danh Sách Username ra List
        public DataSetLocal.MemberDataTable Export()
        {
            GetData();
            return dataSet.Member;
        }
        #endregion

        #region Update User
        public bool UpdateIP(string user, string ip)
        {
            GetData();
            var us2 = dataSet.Member.SingleOrDefault(p => p.Ip == ip);
            if (us2 != null)
                return false;
            else
            {
                dataSet.Member.Single(p => p.Username == user).Ip = ip;
                memberTableAdater.Update(dataSet.Member);
                return true;
            }
        }
        #endregion

    }
}
